<html>
<head>
        <link rel="stylesheet" type="text/css" href="css/widgets.css">
        <link rel="stylesheet" type="text/css" href="css/general.css">
        <link rel="stylesheet" type="text/css" href="css/main.css">
        <link rel="stylesheet" type="text/css" href="css/simplegrid.css">
        <meta name="author" content="Matt Kubilus" />
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <title>Empty Orchestra</title>
        <script type="text/javascript" src="/js/jquery.js"></script> 
        <script type="text/javascript" src="/js/jquery.form.js"></script> 
        <script type="text/javascript" src="/js/json2.js"></script>
        <script type="text/javascript" src="/js/ajax_stuff.js"></script>
        <script type="text/javascript"> 

</script>
</head>
<body>
    <div id="player"></div>

    <script>

    var videoid = 'M7lc1UVf-VE';


$(document).ready(function() {
    var inurl = document.URL;
    //alert(inurl);
    var m = inurl.match(/\?(.*?)=%27(.*?)%27/);
    //alert(m[1] + m[2]);
    if(m[1] == 'songid') {
        videoid = m[2];
    }
    //if(m[1] == 'say_stuff'){
    //    say_stuff(m[2]);
    //}    
});

      // 2. This code loads the IFrame Player API code asynchronously.
      var tag = document.createElement('script');

      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

      // 3. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      var player;
      function onYouTubeIframeAPIReady() {
        //var inurl = document.URL;
        //var m = inurl.match(/\btest.html\?(.*?)='(.*?)'/);
        //alert(m[0]);
        player = new YT.Player('player', {
          height: '600',
          width: '800',
          videoId: videoid,
          playerVars: { 'controls': 1, 'wmode': "opaque", 'rel': 0, 'enablejsapi': 1, 'fs': 1},

          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange,
            'onError': playerError
          }
        });
      }

      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        //alert("Ready!");
        event.target.playVideo();
      }

      // 5. The API calls this function when the player's state changes.
      //    The function indicates that when playing a video (state=1),
      //    the player should play for six seconds and then stop.
      var done = false;
      function onPlayerStateChange(event) {
          if(event.data == YT.PlayerState.ENDED){
            //alert("CLOSE!");
            window.open('', '_self', ''); 
            window.close();
            //window.open('','_self').close();
          }
      }

      function playerError(event) {
        //alert("Player error!");
        window.location.replace('http://www.youtube.com/watch?v='+videoid);
      }
    </script>

</body>
</html>
